<?php
namespace Users\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;

class UserTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function addUser($users,$password)
    {
		if(isset($_SESSION['admin']) && $_SESSION['admin']['user_id']){
			$userid=$_SESSION['admin']['user_id'];
		}else{
			$userid='';
		}
		$password=md5($password);
		$data = array(
			'username' 	  	           => $users['uname'], 	
			'company_name' 	  	       => $users['cname'], 	
			'company_contact_number'   => $users['ccnumber'], 	
			'email' 		           => $users['email'],  		
			'password' 		           => $password,  		
			'phone' 		           => $users['pnumber'],  		
			'country'                  => $users['country'], 
			'state' 		           => $users['state'], 	
			'city' 		               => $users['city'], 	
			'address' 		           => $users['address'], 	
			'type_of_user' 		       => $users['userr_type'], 	
			'u_status'  	           => 0,  	
			'added_date' 	           => date('Y-m-d H:i:s'),   
			'modified_date'	  	       => date('Y-m-d H:i:s'), 	
			'pincode' 		           => $users['pcode'], 
			'admin_user_id' 		   => $userid, 
			
		);	
		$insertresult=$this->tableGateway->insert($data);
		$select = $this->tableGateway->getSql()->select()
				->where('email="'.$data['email'].'"')
				->where('password="'.$data['password'].'"');
		$resultSet = $this->tableGateway->selectWith($select);		
		$row = $resultSet->current();		
		return $row;
		
    }	
	public function checkEmail($email,$type)
    {	
		$select = $this->tableGateway->getSql()->select();			
		$select->where('email="'.$email.'"');
		if($type=='admin'){
			$select->where('user_type="admin"');
		}
		$resultSet = $this->tableGateway->selectWith($select);
		$row = $resultSet->count();
		if (!$row) {
			throw new \Exception("Could not find row $email");
		}
		return $row;		
	}
	public function getdataUsers($data)
    {	
		$select = $this->tableGateway->getSql()->select();
		if($data=='superadmin'){
			$select->where('type_of_user!="superadmin"');
		}else if($data=='users'){
			$select->where('type_of_user="user"');
		}else if($data=='admin'){
			$select->where('type_of_user="admin"');
		}else{
			$select->where('type_of_user!="superadmin"');
		}
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	public function getadminUsers($data)
    {	
		$select = $this->tableGateway->getSql()->select();
		$select->where('type_of_user="'.$data.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	public function checkLogin($user,$authreg='')
    {		
		if($authreg==''){		
			$password=md5($user['userPassword']);
		}else{
			$password=$user['userPassword'];
		}	
		$select = $this->tableGateway->getSql()->select()
				->where('email="'.$user['userEmail'].'"')
				->where('password="'.$password.'"')
				->where('u_status="1"');
		$resultSet = $this->tableGateway->selectWith($select);
		$row = $resultSet->current();
        return $row;
	}
	
	public function checkadminLogin($admin)
    {		
		$password=md5($admin['adminPassword']);
		$select = $this->tableGateway->getSql()->select()
				->where('email="'.$admin['adminUsername'].'"')
				->where('password="'.$password.'"');
		$resultSet = $this->tableGateway->selectWith($select);	
        $row = $resultSet->current();			
        return $row;
	}
	public function editUser($id)
    {
		$select = $this->tableGateway->getSql()->select()			
				->where('user_id= "'.$id.'"');					 
		$resultSet = $this->tableGateway->selectWith($select);	
		$row = $resultSet->current();
        return $row;
	}
	public function getuserDetailes($user_id){	
		$select = $this->tableGateway->getSql()->select();
		$select	->join('country', 'users.country=country.country_id',array('usercountry'=>'nicename'),'left');
		$select	->join('state', 'users.state=state.state_id',array('userstate'=>'state_name'),'left');
		$select->where('users.user_id="'.$user_id.'"');	
		$resultSet = $this->tableGateway->selectWith($select);	
		return $resultSet;				
	}
	public function updateUser($users){	
		$data = array(
			'username' 	  	           => $users['uname'], 	
			'company_name' 	  	       => $users['cname'], 	
			'company_contact_number'   => $users['ccnumber'], 	
			'email' 		           => $users['email'],  		
			'phone' 		           => $users['pnumber'],  		
			'country'                  => $users['country'], 
			'state' 		           => $users['state'], 	
			'city' 		               => $users['city'], 	
			'address' 		           => $users['address'], 	
			'type_of_user' 		       => $users['utype'], 	
			'modified_date'	  	       => date('Y-m-d H:i:s'), 	
			'pincode' 		           => $users['pcode'],
  		
		);	
		$updateuserid=$this->tableGateway->update($data, array('user_id' => $users['hiduserid']));
		return 	$updateuserid;			
	}
	public function updateUserRegAuth($userid){
		$data = array(
				'user_id' 	=>$userid,
				'u_status'  	=>'1',
				);
		$updateuserid=$this->tableGateway->update($data, array('user_id' => $data['user_id']));
		return 	$updateuserid;			
	}	
	public function getpassword($pwd,$userid){ 
		$pwd=md5($pwd);
		$select = $this->tableGateway->getSql()->select();
		$select->where('password="'.$pwd.'"');
		$select->where('user_id="'.$userid.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		$row = $resultSet->count();			
		return $row;
	}
	public function changepwd($pwd,$userid){
		$password=md5($pwd);
		$data = array(
				'user_id'       =>$userid,
				'password'      =>$password,
				);
		$changepassword=$this->tableGateway->update($data, array('user_id' => $data['user_id']));
		return 	$changepassword;			
	}
	public function checkUniqueRecord($email){	
		 $select = $this->tableGateway->getSql()->select()			
			->where('email="'.$email.'"');					 
		$resultSet = $this->tableGateway->selectWith($select);			
		$row=$resultSet->count();		
		return $row;
	}
	public function getMembersByMailId($email){	
		 $select = $this->tableGateway->getSql()->select()			
			->where('email="'.$email.'"');					 
		$resultSet = $this->tableGateway->selectWith($select);		
		return $resultSet;
	}
	public function setPwd($userid,$token){
		$mtoken=md5($token);		
		$data = array(
				'user_id'       =>$userid,
				'password'      =>$mtoken,
				);
		$setNewpassword=$this->tableGateway->update($data, array('user_id' => $data['user_id']));
		return 	$setNewpassword;	
	}
	public function setUsername($userid,$username){
		$data = array(
				'user_id'       =>$userid,
				'username'      =>$username,
				'joindate'      =>date('Y-m-d H:i:s'),
				'last_login'      =>date('Y-m-d H:i:s'),
				);
		$setUsername=$this->tableGateway->update($data, array('user_id' => $data['user_id']));
		return 	$setUsername;	
	}
	public function setUsertype($userid,$user_type){
		$data = array(
				'user_id'       =>$userid,
				'user_type'      =>$user_type,
				);
		$setUsertype=$this->tableGateway->update($data, array('user_id' => $data['user_id']));
		return 	$setUsertype;	
	}
	public function changeStatus($userIds,$value)
	{
		$data = array(
			'state'       =>$value,
		);
		$updateStatus=$this->tableGateway->update($data,array('(user_id IN ('.$userIds.'))'));
		return 	$updateStatus;			
	}
	public function deleteUser($id)
    {
        $result=$this->tableGateway->delete(array('user_id' => $id));
		return  $result;
    }
	public function totalUsers($from,$to){
		$select = $this->tableGateway->getSql()->select();
			$select->columns(array('totalUsers' => new \Zend\Db\Sql\Expression('COUNT(user.user_id)')));
			if($from!=''){
				$select->where('joindate>="'.$from.'"');
				$select->where('joindate<="'.$to.'"');
			}
			$select->where('user.user_type="user"');
			
		$resultSet = $this->tableGateway->selectWith($select);	
		return $resultSet;
	}
	public function totalPublishers($from,$to){
		$select = $this->tableGateway->getSql()->select();
			$select->columns(array('totalPublishers' => new \Zend\Db\Sql\Expression('COUNT(user.user_id)')));
			if($from!=''){
				$select->where('joindate>="'.$from.'"');
				$select->where('joindate<="'.$to.'"');
			}
			$select->where('user.user_type="insitiator"');
		$resultSet = $this->tableGateway->selectWith($select);		
		return $resultSet;
	}
	
	public function updateUserByAdmin($id,$data)
    {
		$data = array(
				'user_id' 	=>$id,
				'user_type' =>$data,
				);
		$updateuserid=$this->tableGateway->update($data, array('user_id' => $id));
		return 	$updateuserid;	
	}
	public function getallusersnames()
	{
		$select = $this->tableGateway->getSql()->select();
		$resultSet = $this->tableGateway->selectWith($select);	
		return $resultSet;
	}
	public function getuserId($name){	
		 $select = $this->tableGateway->getSql()->select()			
			->where('username="'.$name.'"');					 
		$resultSet = $this->tableGateway->selectWith($select);		
		return $resultSet;
	}
	public function deleteUsers($ids){		
		$this->tableGateway->delete(array('(user.user_id IN ('.$ids.'))'));			
		return $this->tableGateway->lastInsertValue;	
	}
	public function getlogin($data)
	{	
	   $password=md5($data['adminPassword']);        
		$select = $this->tableGateway->getSql()->select()
			->where('email="'.$data['adminUsername'].'"')
			->where('password="'.$password.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		$row = $resultSet->current();
		return $row;
	}
}